package com.zline.foodraise.view;

import android.content.Context;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.makeramen.roundedimageview.RoundedImageView;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.zline.foodraise.R;
import com.zline.foodraise.entity.PictureBean;
import com.zline.foodraise.entity.ProductBean;
import com.zline.foodraise.listener.RecyclerItemClicked;
import com.zline.foodraise.util.ImageUtil;

import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EViewGroup;
import org.androidannotations.annotations.ViewById;

import java.util.List;

/**
 * Created by heshantao on 15/6/27.
 * <p/>
 * EViewGrop外层最好用Reclativelayout 否则martch_parent 会出问题当用RecyleView
 * me碎片中ViewPage商品Item
 */
@EViewGroup(R.layout.me_viewpage_card_item)
public class ProductViewPageCardItem extends RelativeLayout {
    private static final String LOG_TAG = ProductViewPageCardItem.class.getSimpleName();
    @ViewById(R.id.view_page_card_item_name_tv)
    TextView name_tv;
    @ViewById(R.id.view_page_card_item_sales_tv)
    TextView sales_tv;
    @ViewById(R.id.view_page_card_item_price_tv)
    TextView price_tv;
    @ViewById(R.id.viewpage_cardview_item_riv)
    RoundedImageView iv;
    @Bean
    ImageUtil imageUtil;
    DisplayImageOptions project_icon_options;
    ImageLoader imageLoader;
    int position;
    RecyclerItemClicked itemClicked;
    Context cxt;

    public ProductViewPageCardItem(Context context) {
        super(context);
        this.cxt=context;
    }

    public void bind(ProductBean product, int position) {
        this.position = position;
        String pic_url = null;
        if (product != null) {
            imageLoader = ImageLoader.getInstance();
            project_icon_options = imageUtil.
                    getDisplayImageOptions(R.mipmap.default_product_icon,
                            R.mipmap.default_product_icon, R.mipmap.default_product_icon);
            List<PictureBean> pictureBeanList = product.getPictureBeanList();
            for (PictureBean pictureBean : pictureBeanList) {
                if (pictureBean.getIsMain() == 1) {
                    pic_url = pictureBean.getPictureUrl();
                    break;
                }
            }
            imageLoader.displayImage(pic_url, iv, project_icon_options);
            name_tv.setText(product.getProductName());
            String  saleStr=cxt.getResources().getString(R.string.project_sales, product.getSales());
            sales_tv.setText(saleStr);
            double price=product.getMinPrice();
            price_tv.setText("￥"+price);
        }
    }

    public void setListener(RecyclerItemClicked listener) {
        this.itemClicked = listener;
    }

    @Click(R.id.me_viewpage_cardView)
    void OnClick() {
        itemClicked.onRecyclerItemClicked(position);
    }
}
